<!--
 * @Author: lvye
 * @Description: 
 * @Date: 2023-08-23 11:03:04
 * @LastEditTime: 2023-08-30 11:42:59
 * @FilePath: \vue3-ts\src\base-ui\table\src\table.vue
-->
<template>
  <div class="hy-table">
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      border
      style="width: 100%"
      v-bind="childrenProps"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
        width="55"
        align="center"
      />
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <!-- <template #default="scope">是el-table组件的插槽,通过scope接收属性,scope里面有row这一参数,值为当前行的数据 -->
          <template #default="scope">
            <!-- 设置 hy-table插槽 通过row属性向外传递数据,row参数名可变,内外对应即可-->
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer">
      <slot name="footer">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="page.pageSize"
          :current-page="page.currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from "vue"

export default defineComponent({
  props: {
    title: {
      type: String,
      default: ""
    },
    listData: {
      type: Array,
      required: true
    },
    listCount: {
      type: Number,
      default: 0
    },
    propList: {
      type: Array,
      default: () => []
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 0, pageSize: 10 })
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    }
  },
  setup(props, { emit }) {
    const handleCurrentChange = (val: number) => {
      console.log(val)
      emit("update:page", { ...props.page, currentPage: val })
    }
    const handleSizeChange = (val: number) => {
      console.log(val)
      emit("update:page", { ...props.page, pageSize: val })
    }
    return {
      handleCurrentChange,
      handleSizeChange
    }
  }
})
</script>

<style lang="less" scoped>
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}
.footer {
  display: flex;
  justify-content: flex-end;
  padding: 10px 0;
}
</style>
